package com.chuangyang.fixboxmaster.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.android.volley.Response;
import com.chuangyang.fixboxlib.widgets.ListItemView;
import com.chuangyang.fixboxmaster.AppData;
import com.chuangyang.fixboxmaster.R;
import com.chuangyang.fixboxmaster.bean.BaseInfo;
import com.chuangyang.fixboxmaster.service.FixBoxApi;
import com.chuangyang.fixboxmaster.service.GsonRequest;
import com.chuangyang.fixboxmaster.utils.PrefUtils;
import com.umeng.update.UmengUpdateAgent;

/**
 * Created by vanshu on 15/3/17 16:02.
 */
public class MoreSettingActivity extends BaseActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_more_setting);
        ListItemView mAgreement = (ListItemView) findViewById(R.id.btn_agreement);
        ListItemView mCheckUpdate = (ListItemView) findViewById(R.id.btn_check_update);
        ListItemView mAbout = (ListItemView) findViewById(R.id.btn_about);
        Button mLogout = (Button) findViewById(R.id.btn_logout);
        mAgreement.setOnClickListener(this);
        mCheckUpdate.setOnClickListener(this);
        mAbout.setOnClickListener(this);
        mLogout.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_agreement:
                startActivity(new Intent(MoreSettingActivity.this, AgreementActivity.class));
                break;
            case R.id.btn_check_update:
                UmengUpdateAgent.forceUpdate(MoreSettingActivity.this);
                break;
            case R.id.btn_about:
                startActivity(new Intent(MoreSettingActivity.this, AboutActivity.class));
                break;
            case R.id.btn_logout:
                addRequest(new GsonRequest<>(FixBoxApi.USER_LOGOUT, BaseInfo.class,responseListener));
                break;
        }
    }

    private Response.Listener<BaseInfo> responseListener=new Response.Listener<BaseInfo>() {
        @Override
        public void onResponse(BaseInfo response) {
            Toast.makeText(getApplicationContext(),"退出成功",Toast.LENGTH_SHORT).show();

            PrefUtils.putString(AppData.getContext(),PrefUtils.ACCESS_TOKEN,null);
            Intent intent = new Intent(MoreSettingActivity.this, LoginActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            startActivity(intent); // Launch the LoginActivity
            finish();         // Close down the SettingsActivity
        }
    };

}
